﻿using System;
using System.Collections.Generic;
using System.EnterpriseServices;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DataLib.Accounts.Bus;
using DataLib.BLL;
using BarCode = DataLib.BLL.BarCode;
using InStorage = DataLib.BLL.InStorage;
using OutStorage = DataLib.Model.OutStorage;

namespace Ewts.Production
{
    public partial class ProduceStorage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Session["UserInfo"] != null)
                {
                    User curuser =new User();
                    curuser = (User)Session["UserInfo"];
                    bool flag =  SetDisplay();
                    if (flag == false)
                    {
                        DataLib.Common.MessageBox.ShowAndRedirect(Page, "非常URL进入，请从列表进入", "ProduceWaitInStorageList.aspx");
                    }
                   
                }
       
            }
        }
        protected bool SetDisplay()
        {
            bool flag = true;
            if (!string.IsNullOrEmpty(Request["outid"]))
            {
                flag = bindOutid(Request["outid"]);
            }

            else if (!string.IsNullOrEmpty(Request["insid"]))
            {
                DataLib.BLL.InStorage bll = new InStorage();
                DataLib.Model.InStorage model = new DataLib.Model.InStorage();
                model = bll.GetModel(int.Parse(Request["insid"]));
            
                flag = bindOutid(model.outno);
            }
            return flag;

        }
        private bool bindOutid(string outid)
        {
            bool flag = true;
            DataLib.BLL.Productions bll = new Productions();
            DataLib.Model.OutStorage outModel = new DataLib.Model.OutStorage();
            ////DataLib.Model.lingniaoItems lingModel = new DataLib.Model.lingniaoItems();
            DataLib.Model.InStorage insmodel = bll.GetRsidByOutid(int.Parse(outid));

            DataLib.BLL.OutStorage outBll = new DataLib.BLL.OutStorage();
            DataLib.BLL.InStorage inBll = new DataLib.BLL.InStorage();
            

            outModel = outBll.GetModel(int.Parse(outid));
            if (outModel.status == 13)
            {
                inputLoadNum.InnerHtml = outModel.VLoadNum;
                txtSrcOutSid.InnerHtml = outModel.outStorageNo;
                hidInDepart.Value = outModel.belogarea;
                hidOutDepart.Value = outModel.graphicCity;

                if (insmodel != null)
                {
                    hidinsid.Value = insmodel.id.ToString();
                    hidwrlno.Value = insmodel.wrlno;
                    labFlNo.InnerHtml = insmodel.flno;
                    labShNo.InnerHtml = insmodel.shno;
                    labRSNo.InnerHtml = insmodel.wrlno;
                    spanWrlNo.InnerHtml = insmodel.wrlno;
                    ShowFLData((int)insmodel.id);
                    if (bll.GetFinshed((int)insmodel.id, (int)outModel.id) == true)
                    {
                        btnSave.Enabled = true;
                    }
                    else
                    {
                        btnSave.Enabled = false;
                    }

                }
                else
                {
                    hidinsid.Value = "0";
                    hidwrlno.Value = "";
                    btnSave.Enabled = false;
                }
                hiddenOper.Value = outModel.Operator;
                hidmonth.Value = outModel.clearmonth;
                hidoutid.Value = outid;
                spanTotal.InnerHtml = outModel.totalWeight.ToString();
                //            StrProductionBalance=ShowProductionBalance(Convert.ToInt32(outid),outModel.clearmonth);

                ShowBarCodeData((int)outModel.id);
            }
            else
            {
                flag = false;
            }
            return flag;
        }

        /// <summary>
        /// 获取BarCode是否锁定状态
        /// </summary>
        /// <param name="islock"></param>
        /// <returns></returns>
        protected string GetIsLock(string islock)
        {
            if (Boolean.Parse(islock))
                return "checked";
            else
            {
                return "";
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string outid = hidoutid.Value;
            string month = hidmonth.Value;

      
            DataLib.BLL.Productions bllpro = new Productions();
            int insid =bllpro.LuRuSubmit(Convert.ToInt32(outid));
            if (insid != -1)
            {
                DataLib.Common.MessageBox.ShowAndRedirect(Page, "提交成功，请选择打印", "SelectReport.aspx?insid=" + insid.ToString());
                //DataLib.Common.MessageBox.ShowAndRedirect(Page, "提交成功待审核！", "/Production/ProduceWaitInStorageList.aspx");
            }
            else
            {
                DataLib.Common.MessageBox.Show(this, "提交失败!");
            }
        }

        protected void btnCanel_Click(object sender, EventArgs e)
        {
          //  DataLib.Common.MessageBox.ShowAndRedirect(Page, "跳转测试", "/Production/ProduceWaitInStorageList.aspx");
        }

      

        private void ShowFLData(int instroingid)
        {
            //辅料绑定
            DataTable dtfl = new DataLib.BLL.ins_fuliao().GetList("instroageid=" + instroingid).Tables[0];
            reptFl.DataSource = dtfl;
            reptFl.DataBind();

            //损耗绑定
            DataTable dtsh = new DataLib.BLL.ins_sunhao().GetList("instroageid=" + instroingid.ToString().Trim()).Tables[0];
            reptSh.DataSource = dtsh;
            reptSh.DataBind();
        }

        private void ShowBarCodeData(int outid)
        {
            DataLib.BLL.Productions bll = new Productions();

            spanlf.InnerHtml = bll.GetStrFormulae(outid, "W");
            spanrs.InnerHtml = bll.GetStrFormulae(outid, "S");
            DataTable dt=new DataTable();
            dt = bll.GetBarcodeList(outid,"RS").Tables[0];

            rept.DataSource = dt;
            rept.DataBind();
        }


    }
}